﻿#region System Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
#endregion System Namespace

#region Foreign Namespace
#endregion Foreign Namespace

#region Styx Namespace

using System.Windows;
using System.Windows.Forms;
using Styx;
using Styx.Helpers;
using Styx.Plugins;
using Styx.Common;
using Styx.CommonBot;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using MessageBox = System.Windows.MessageBox;
using MessageBoxOptions = System.Windows.MessageBoxOptions;

#endregion Styx Namespace

namespace ExtraLazyRaider
{
    public class ExtraLazyRaider : HBPlugin
    {
        public override string Name
        {
            get { return "ExtraLazyRaider"; }
        }

        public override string Author
        {
            get { return "Giwin"; }
        }

        public override Version Version
        {
            get { return new Version(1, 0); }
        }

        public override bool WantButton
        {
            get { return true; }
        }

        public override void Initialize()
        {
            Lua.Events.AttachEvent("LFG_PROPOSAL_SHOW", LfgDungeonReady);
            Lua.Events.AttachEvent("READY_CHECK", Ready);
            Lua.Events.AttachEvent("LFG_ROLE_CHECK_SHOW", LfgRoleCheckShow);
            Lua.Events.AttachEvent("RESURRECT_REQUEST", Resurrect);
            Logging.Write("ExtraLazyRaider - Loaded Version " + Version);
        }

        public void Resurrect(object sender, LuaEventArgs args)
        {
            Random r = new Random();
            Logging.Write("[ResurrectRequest]: 'Ressurect Request' menu detected, clicking Accept in 1.5 - 3.5 seconds");
            Thread.Sleep(r.Next(1500, 3500));
            Lua.DoString("RunMacroText(\"/click StaticPopup1Button1\");");
        }
        public void Ready(object sender, LuaEventArgs args)
        {
            Random r = new Random();
            Logging.Write("[ReadyCheck]: 'Ready Check' menu detected, clicking Ready in 1.5 - 3.5 seconds");
            Thread.Sleep(r.Next(1500, 3500));
            Lua.DoString("RunMacroText(\"/click ReadyCheckFrameYesButton\");");
        }

        public void LfgRoleCheckShow(object sender, LuaEventArgs args)
        {
            Random r = new Random();
            Logging.Write("[RollCheck]: 'Role Check' menu detected, clicking Ready in 2.5 - 4.5 seconds");
            Thread.Sleep(r.Next(500, 1000));

            #region

            //RolePollPopupRoleButtonDPS
            //   RolePollPopupRoleButtonHealer 
            //RolePollPopupRoleButtonTank

            #endregion

            Lua.DoString("RunMacroText(\"/click RolePollPopupRoleButtonDPS\");");
            Thread.Sleep(r.Next(2500, 4500));
            Lua.DoString("RunMacroText(\"/click RolePollPopupAcceptButton\");");

        }

        public void LfgDungeonReady(object sender, LuaEventArgs args)
        {
            //MessageBox.Show("Dungeon Menu Popup Detected!, Joining Dungeon", "ExtraLazyRaider", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
            Random r = new Random();
            Logging.Write("[DungeonCheck]: 'Dungeon Proposal' menu detected, clicking 'Enter' Dungeon Button in 3.5 - 5.5 seconds");
            Thread.Sleep(r.Next(3500, 5500));
            Lua.DoString("RunMacroText(\"/click LFGDungeonReadyDialogEnterDungeonButton\");");
        }
        // Name of Mobs who we have issues with - 
        // Sha of Fear (Object Interact return)
        // Elegron (Platform Drop)
        //
        public override void Pulse()
        {
            if (StyxWoW.IsInGame)
                if (StyxWoW.Me.IsAlive)

                    /// Replace StyxWoW.Me.RaidMembers[8] with something more reliable and stable
            while (!StyxWoW.Me.Combat &&
                StyxWoW.Me.RaidMembers[0].Distance > 5.5 &&
                StyxWoW.Me.RaidMembers[0].IsMoving &&
                StyxWoW.Me.IsInInstance)
            {
                WoWMovement.ClickToMove(StyxWoW.Me.RaidMembers[0].Location);
            }
            if (StyxWoW.Me.RaidMembers.Count < 5 && StyxWoW.Me.IsInInstance)
            {
                Logging.Write(StyxWoW.Me.RaidMembers.Count.ToString());
                Logging.Write("End of Raid? Will Sleep for 60 seconds and check again to make sure");
                Thread.Sleep(60000);
                if (StyxWoW.Me.RaidMembers.Count < 5 && StyxWoW.Me.IsInInstance)
                {
                    Logging.Write("Leaving Dungeon in 5 Seconds..");
                    Thread.Sleep(4000);
                    Lua.DoString("RunMacroText(\"/click QueueStatusMinimapButton\");");
                    Thread.Sleep(1000);
                    Lua.DoString("RunMacroText(\"/click DropDownList1Button3\");");
                }
            }
            if (StyxWoW.Me.RaidMembers[0].CurrentTarget != null &&
                StyxWoW.Me.RaidMembers[0].Combat &&
                StyxWoW.Me.IsInInstance)
            {
                StyxWoW.Me.RaidMembers[0].CurrentTarget.Target();
                if (StyxWoW.Me.RaidMembers[0].CurrentTarget != null &&
                    StyxWoW.Me.CurrentTarget != null &&
                    StyxWoW.Me.CurrentTarget == StyxWoW.Me.RaidMembers[3].CurrentTarget &&
                    StyxWoW.Me.IsInInstance)
                {
                    if (!StyxWoW.Me.CurrentTarget.IsWithinMeleeRange)
                    {
                        if (!StyxWoW.Me.IsSafelyFacing(StyxWoW.Me.CurrentTarget))
                            StyxWoW.Me.Interact();
                    }
                }
            }
        }
    }
}
   